#include "TR.h"

TR::TR(String id, Sensor& s, Relay& r) : 
Controller(id), _sensor(s), _relay(r) {
  _delta = 2;
  _minTemp = 5;
}

void TR::setCommand(String command) {
  if(command.equalsIgnoreCase("ON")) {
    _relay.on();
    _minTemp = -1;
  }
  else if(command.equalsIgnoreCase("OFF")) {
    _relay.off();
    _minTemp = -1;
  } 
  else {
    _minTemp = constrain(command.toInt(), 5, 30);
  }
}

boolean TR::check() {
  if(_minTemp < 0) {
    return false;
  }
  int curTemp = _sensor.getTemperature();
  if(curTemp < _minTemp - _delta) {
    _relay.on();
  }
  else if(curTemp >= _minTemp + _delta) {
    _relay.off();
  }
  return false;
}

int TR::getMinTemp() {
  return _minTemp;
}

Sensor& TR::getSensor() {
  return _sensor;
}

Relay& TR::getRelay() {
  return _relay;
}

String TR::getStatus() {
  return getId() + "(MT=" + _minTemp + "," + _sensor.getStatus() + "," + _relay.getStatus() + ")";
}


